/**
 * This project create for studying propose at BUPT University. You can change or reuse this project anywhere
 */

package edu.bupt.ooad.library.ui;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import edu.bupt.ooad.library.Book;
import edu.bupt.ooad.library.BookDescription;

/**
 * @author Hamed Abdollahpour
 */
public class BookTableModel extends AbstractTableModel {

    private final static String[] COLUMNS = new String[] {
            "Available", "ID", "category", "Title", "Author", "ISBN"
    };

    private List<Book> books;

    public BookTableModel(List<BookDescription> bookDescriptions) {
        if (bookDescriptions != null) {
            this.books = Tools.listAllBooks(bookDescriptions);
        } else {
            this.books = new ArrayList<Book>();
        }
    }

    public Book getBook(int row) {
        return books.get(row);
    }

    public void removeRow(int row) {
        books.remove(row);

        fireTableRowsDeleted(row, row);
    }

    @Override
    public String getColumnName(int column) {
        return COLUMNS[column];
    }

    @Override
    public int getColumnCount() {
        return COLUMNS.length;
    }

    @Override
    public int getRowCount() {
        return books.size();
    }

    @Override
    public Object getValueAt(int row, int column) {
        Book book = books.get(row);

        switch (column) {
            case 0:
                return book.isAvailable();
            case 1:
                return book.getBookId();
            case 2:
                return book.getDescription().getCategory();
            case 3:
                return book.getDescription().getTitle();
            case 4:
                return book.getDescription().getAuthor();
            case 5:
                return book.getDescription().getIsbn();

            default:
                return null;
        }
    }

}
